package com.justech.eat.service.daemons.impl;

import com.justech.eat.entity.User;
import com.justech.eat.service.UserService;
import com.justech.eat.service.component.UserDataSupplier;
import com.justech.eat.service.daemons.SysUserSynchronizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: eat-service
 * @description: 默认的用户同步器实现
 * @author: feifei.li
 * @create: 2020-06-10 09:08
 **/
@Component
public class DefaultSysUserSynchronizer implements SysUserSynchronizer {
    final static Logger logger = LoggerFactory.getLogger(DefaultSysUserSynchronizer.class);
    @Autowired
    UserDataSupplier userDataSupplier;
    @Autowired
    UserService userService;


    /*获取erp中的用户信息*/
    protected List<User> getErpUserList() {
        /*String userErpUrl = configService.get("user_erp_url").getValue();
        String data = HttpClientUtil.doGet(userErpUrl);
        ArrayList<User> User = null;
        try {
            JSONArray jsonArray = JSONObject.parseArray(data);
            User = new ArrayList<>();
            for (Object o : jsonArray) {
                User eSysUser = JSONObject.toJavaObject(JSONObject.parseObject(o.toString()), User.class);
                User.add(eSysUser);
            }
        } catch (Exception e) {
            logger.error("获取用户信息失败！[form erp]");
            logger.error(userErpUrl);
            e.printStackTrace();
        }*/
        return userDataSupplier.getUserData();
    }

    /*获取erp中的用户map*/
    protected Map<String, User> getErpUserMap(List<User> User) {
        Map<String, User> eSysUserMap = new ConcurrentHashMap<>(User.size());
        User.stream().forEach(e -> eSysUserMap.put(e.getPin(), e));
        return eSysUserMap;
    }

    /*获取本地用户列表*/
    protected List<User> getLocationUserList() {
        return userService.getAll();
    }

    /*将列表转换成map*/
    protected Map<String, User> getLocationUserMap(List<User> sysUsers) {
        Map<String, User> sysUserMap = new ConcurrentHashMap<>(sysUsers.size());
        sysUsers.stream().forEach(e -> sysUserMap.put(e.getPin(), e));
        return sysUserMap;
    }

    /*查询新来的*/
    protected ArrayList<User> findIncomingFromErp(List<User> User, Map<String, User> locationUsers) {
        ArrayList<User> incoming = new ArrayList<>();
        for (User eSysUser : User) {
            User e = locationUsers.get(eSysUser.getPin());
            if (e == null) {
                User locationUser = new User();
                BeanUtils.copyProperties(eSysUser, locationUser);
                incoming.add(locationUser);
            }
        }
        return incoming;
    }

    protected ArrayList<User> findResignedFromLocation(List<User> locationUsers, Map<String, User> eSysUserMap) {
        ArrayList<User> resigned = new ArrayList<>();
        for (User locationUser : locationUsers) {
            User eSysUser = eSysUserMap.get(locationUser.getPin());
            if (eSysUser == null) {
                resigned.add(locationUser);
            }
        }
        return resigned;
    }


    @Override
    public void synchronization() {

        List<User> erpUserList = getErpUserList();
        Map<String, User> erpUserMap = getErpUserMap(erpUserList);

        List<User> locationUserList = getLocationUserList();
        Map<String, User> locationUserMap = getLocationUserMap(locationUserList);

        /*寻找新来的同事*/
        ArrayList<User> incoming = findIncomingFromErp(erpUserList, locationUserMap);
        /*寻找已离职的同事*/
        ArrayList<User> resigned = findResignedFromLocation(locationUserList, erpUserMap);

        /*将新来的数据插入本地数据库*/
        insert(incoming);
        /*将已离职的数据从本地数据库删除*/
        remove(resigned);
    }

    protected void insert(List<User> incoming) {
        userService.saveAll(incoming);
    }

    protected void remove(List<User> resigned) {
        int resignation = userService.acceptResignation(resigned);
        System.out.println("已删除部分用户，数量为：" + resignation);
    }


}
